//
//  File.swift
//  d11
//
//  Created by mac on 2018/6/10.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import Foundation
import UIKit
class TabSupportViewTwoController:UIViewController{
  
    
    var backgroundView = UIView()
    var htmlImageView: UIView?
    var appImageView: UIView?
    var balanceButton: UIButton?
    var vercitalLine:UIView?
    var csUrl: String?
       var atcUrl: String?
    override func viewDidLoad() {
        
          super.viewDidLoad()
        
        /**** Balance ****/
        let balanceButton = UIButton(type: .custom)
        balanceButton.setTitle("Fetchin", for: .normal)
        balanceButton.setImage(UIImage(named: "icon_nav_right_amount"), for: .normal)
        balanceButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 0)
        balanceButton.setTitleColor(UIColor.white, for: .normal)
        balanceButton.titleLabel?.font = UIFont(name: balanceButton.titleLabel!.font.fontName, size: 14)
        self.balanceButton = balanceButton
        
        let barItem = UIBarButtonItem(customView: balanceButton)
        
        self.navigationItem.setRightBarButton(barItem, animated: true)
        balanceButton.addTarget(self, action: #selector(self.pushWebViewTransfer), for: .touchUpInside)
        /*** Balance ***/
        
        
        let topLogo = UIImage(named: "icon_nav_left_logo")
        let titleView = UIView()
        let topLogoView = UIImageView(image: topLogo)
        titleView.addSubview(topLogoView)
        topLogoView.contentMode = .scaleToFill
        topLogoView.frame = CGRect(x: -(137/2), y:-(35/2), width: 137, height: 35)
        navigationItem.titleView = titleView
     
        //  navigationItem.rightBarButtonItem = rightItem
        self.navigationController?.navigationBar.tintColor = UIColor.black
        refreshBalance()
        //self.view.addSubview(navigationBar)
        //self.view.addSubview(containerView)
        backgroundView = UIView(frame:CGRect(x:10,y:200,width:UIHelper.fullSize.width-20,height:192))
        backgroundView.backgroundColor=UIColor.white;
        self.view.addSubview(backgroundView)
        let titleLabel = UILabel(frame: CGRect(x: 10, y: 200, width: UIHelper.fullSize.width-20, height: 28))
        titleLabel.font = UIFont(name: titleLabel.font.fontName, size: CGFloat(15))
        titleLabel.backgroundColor=UIColor.white;
        titleLabel.textColor=UIColor.init(red: 153/255, green: 153/255, blue: 153/255, alpha: 1)
        titleLabel.text = "选择客服:"
        titleLabel.textAlignment = .left
        self.view.addSubview(titleLabel)
        let separatorLineView = UIView()
        separatorLineView.frame.origin = CGPoint(x: 10, y: titleLabel.frame.height + 200)
        separatorLineView.frame.size = CGSize(width: UIHelper.fullSize.width-20, height: 0.5)
        separatorLineView.backgroundColor = UIColor.init(white: 0.85, alpha: 1)
        self.view.addSubview(separatorLineView)
        let imageStack = TZStackView()
        imageStack.axis = .horizontal
        imageStack.distribution = .fillEqually
        imageStack.spacing = 80
        imageStack.frame.origin = CGPoint(x: 40, y: separatorLineView.frame.height + separatorLineView.frame.origin.y+6)
        imageStack.backgroundColor=UIColor.white
        imageStack.frame.size = CGSize(width: UIHelper.fullSize.width - 80 , height: 124)
        
        let image1 = UIImageView(image: UIImage(named: "heper_self"))
        let image2 = UIImageView(image: UIImage(named: "atificial"))
        
        image1.contentMode = .scaleAspectFit
        image2.contentMode = .scaleAspectFit
        
        imageStack.addArrangedSubview(image1)
        imageStack.addArrangedSubview(image2)
        
        image1.anchors.heightAnchor.constraintEqualToConstant(c: image1.frame.width).isActive = true
        image2.anchors.heightAnchor.constraintEqualToConstant(c: image2.frame.width).isActive = true
        
        imageStack.frame.size = CGSize(width:UIHelper.fullSize.width - 80 , height: image1.frame.height - 36)
        self.view.addSubview(imageStack)
        
        htmlImageView = image1
        appImageView = image2
    
           let buttonStack = TZStackView()
            buttonStack.axis = .horizontal
            buttonStack.distribution = .fillEqually
            buttonStack.frame.origin = CGPoint(x: 10, y: titleLabel.frame.height + 8 + separatorLineView.frame.height + 8 + imageStack.frame.height+200)
            buttonStack.frame.size = CGSize(width: UIHelper.fullSize.width - 20, height: 40)
        let leftButton = UIButton()
        let rightButton = UIButton()
        
        leftButton.setTitle("自助客服", for: .normal)
        //leftButton.setBackgroundImage(UIImage(named: "btn_bg_left"), for: .normal)
        leftButton.backgroundColor=UIColor.init(red: 251/255, green: 170/255, blue: 62/255, alpha: 1)
        leftButton.setTitleColor(UIColor.init(red: 0.85, green: 0.82, blue: 0.79, alpha: 1), for: .normal)
        leftButton.addTarget(self, action: #selector(continueWithCsSelf(sender:)), for: .touchUpInside)
        
        rightButton.setTitle("人工客服", for: .normal)
      //  rightButton.setBackgroundImage(UIImage(named: "btn_bg_right"), for: .normal)
         rightButton.backgroundColor=UIColor.init(red: 251/255, green: 170/255, blue: 62/255, alpha: 1)
        rightButton.setTitleColor(UIColor.init(red: 0.85, green: 0.82, blue: 0.79, alpha: 1), for: .normal)
        rightButton.addTarget(self, action: #selector(continueWithCsAtc(sender:)), for: .touchUpInside)
        
                buttonStack.addArrangedSubview(leftButton)
         buttonStack.addArrangedSubview(rightButton)
            self.view.addSubview(buttonStack)
        vercitalLine=UIView(frame:CGRect(x: (UIHelper.fullSize.width-0.5)/2, y: 228, width: 0.5, height: 175))
        vercitalLine?.backgroundColor=UIColor.init(white: 0.85, alpha: 1)

        self.view.addSubview(vercitalLine!)
        
    }
    @objc func continueWithCsSelf(sender: Any){
        if UserHelper.getLoginStatus() {
            if let serviceSelfIurl=GlobalInfo.serviceSelfIurl{
                self.atcUrl=serviceSelfIurl
            }else{
                self.atcUrl="https://\(UserDefaults.standard.string(forKey: DefaultKey.USER_DOMAIN) ?? "" )/walletm/csIndex?isMobile=1"
            }
            self.navigateWebView(toPath: self.atcUrl!, "自助客服",sign: true)
        }else{
            LoginViewController.loginIfNeedController(currentController: self) {}
        }
    }
    @objc func continueWithCsAtc(sender: Any){
        if UserHelper.getLoginStatus() {
            if let maintainData = GlobalInfo.gameMaintainData {
                self.csUrl = maintainData["maintain_cs_url"]!
            }else{
                if let siteData = GlobalInfo.siteInfoData {
                    if let supportURL = siteData["support"] as? String {
                        self.csUrl = supportURL
                    }else {
                        
                        self.csUrl = GlobalInfo.defaultCSUrl
                    }
                }
                if let serviceIurl=GlobalInfo.serviceIurl {
                    self.csUrl=serviceIurl
                }
            }
            self.navigateWebView(toPath: csUrl!, "人工客服",sign: true)
        }else{
            LoginViewController.loginIfNeedController(currentController: self) {}
        }
    }
    func refreshBalance(){
        if UserHelper.getLoginStatus() {
            if let withBalance=GlobalInfo.memberInfoData!["balance"] as? String {
                var  balance: Double = Double(withBalance)!
                self.balanceButton?.setTitle(String(format: "%.2f", balance), for: .normal)
                let size = self.balanceButton!.intrinsicContentSize
                balanceButton!.frame.size = CGSize(width: size.width, height: 33)
                
            }
        }else{
            let button1 = UIBarButtonItem(title: "注册/登录", style: .plain, target: self, action: #selector(self.loginRegisterButtonClick))
            button1.tintColor = UIColor.orange
            
            self.navigationItem.setRightBarButtonItems([button1], animated: false)
        }
    }
    /**** Others ****/
    func navigateWebView(toPath: String, _ title: String,sign:Bool){
        let goNext: WebViewController = WebViewController.init()
        let url = toPath
        goNext.url = url
        goNext.title = title
        goNext.signLoging=sign
        goNext.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(goNext, animated: true)
    }
    @objc func pushWebViewTransfer(){
        if  GlobalInfo.isPlatformMainhtml == nil || GlobalInfo.isPlatformMainhtml != false{
            self.navigateWebView(toPath: "/walletm/transfer", "",sign: true)
        }else{
            let vc = MainAccountVC()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    @objc func loginRegisterButtonClick(){
         setRootLoginVC()
       // GlobalInfo.mainTabBarController?.navigationController?.popToRootViewController(animated: true)
              self.dismiss(animated: true, completion: nil)
    }
}
